package com.soft.mainsite.component.beetl.tag.cms;


import com.soft.mainsite.common.utils.CmsUtil;
import com.soft.mainsite.module.web.cms.service.ModelFiledService;
import com.soft.mainsite.module.web.cms.vo.ModelFiledVo;
import com.soft.mainsite.mybatis.model.TCmsModelFiled;
import com.soft.mainsite.common.exception.CmsException;
import org.beetl.core.GeneralVarTagBinding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Description:SearchModelFiledValueTag
 *
 * @author Jin
 * @create 2017-06-05
 **/
@Service
@Scope("prototype")
public class SearchModelFiledValueTag extends GeneralVarTagBinding {


    @Autowired
    private ModelFiledService modelFiledService;

    @Value("${system.site.prefix}")
    private String sitePrefix;

    private static final String RADIO = "radio";
    @SuppressWarnings("unchecked")
    @Override
    public void render() {
        HttpServletRequest request = (HttpServletRequest) ctx.getGlobal("request");
        Integer siteId = (this.getAttributeValue("siteId") instanceof String) ? Integer.parseInt((String) this.getAttributeValue("siteId")) : (Integer) this.getAttributeValue("siteId");
        Long catId = (this.getAttributeValue("categoryId") instanceof String) ? Long.parseLong((String) this.getAttributeValue("categoryId")) : (Long) this.getAttributeValue("categoryId");
        Integer modelId = (this.getAttributeValue("modelId") instanceof String) ? Integer.parseInt((String) this.getAttributeValue("modelId")) : (Integer) this.getAttributeValue("modelId");
        String filedName = (String) this.getAttributeValue("filedName");
        Map<String, Object> param = (Map<String, Object>) this.getAttributeValue("param");
        TCmsModelFiled modelFiled = modelFiledService.findModelFiledByFiledName(filedName);
        if (CmsUtil.isNullOrEmpty(modelFiled)) {
            throw new CmsException("模型字段不存在！");
        }
        String url = request.getContextPath() + "/search?s=" + siteId + "&m=" + modelId + "&c=" + catId + "&"
                + filedName + "={filedValue}";
        String[] values = modelFiled.getFiledValue().split("#");
        String str = "";
        String kv = "";
        boolean flag = true;
        if (RADIO.equals(modelFiled.getFiledClass())) {
            for (String value : values) {
                ModelFiledVo mfv = new ModelFiledVo();
                if (param != null) {
                    if (!param.isEmpty()) {
                        for (Entry<String, Object> entry : param.entrySet()) {
                            if (!filedName.equals(entry.getKey())) {
                                if (!kv.equals(entry.getKey() + entry.getValue())) {
                                    str += "&" + entry.getKey() + "=" + entry.getValue();
                                }
                                kv = entry.getKey() + entry.getValue();
                            }
                        }
                    }
                }
                if (flag) {
                    mfv.setUrl(request.getContextPath() + "/search?m=" + modelId + "&c="
                            + catId + str);
                    mfv.setName("全部");
                    this.binds(mfv);
                    this.doBodyRender();
                    flag = false;
                }
                mfv.setUrl(url.replace("{filedValue}", value) + str);
                mfv.setName(value);
                this.binds(mfv);
                this.doBodyRender();

            }
        } else {
            ModelFiledVo mfv = new ModelFiledVo();
            mfv.setUrl("#");
            mfv.setName(modelFiled.getFiledValue());
            this.binds(mfv);
            this.doBodyRender();
        }
    }

    public static void main(String[] args) {
        String l = "http://127.0.0.1:8080/search?m=48&c=5&test=&test=qwe";
        System.out.println(l.contains("test"));
    }

}
